package com.sniper.springmvc.mybatis.service.impl;

import com.sniper.springmvc.model.AdminGroup;
import com.sniper.springmvc.model.AdminUser;
import com.sniper.springmvc.model.AdminUserGroup;
import com.sniper.springmvc.mybatis.dao.BaseDao;
import com.sniper.springmvc.utils.UserDetailsUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shiro.authc.CredentialsException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("adminUserService")
public class AdminUserServiceImpl extends BaseServiceImpl<AdminUser> implements AdminUserService {

    @Resource
    AdminGroupService groupService;

    @Resource
    AdminUserGroupService adminUserGroupService;

    @Resource
    AdminRightService rightService;


    @Override
    @Resource(name = "adminUserDao")
    public void setDao(BaseDao<AdminUser> dao) {
        super.setDao(dao);
    }

    @Override
    public AdminUser validateByName(String username) {
        Map<String, Object> params = new HashMap<>();
        params.put("name", username);
        List<AdminUser> adminUser = this.query("find", params);
        if (adminUser.size() > 0) {
            return adminUser.get(0);
        }
        return null;
    }

    @Override
    public AdminUser getUserAndGroupAndRight(String username) {
        AdminUser adminUser = this.get("get_user_by_name", username);
        List<AdminGroup> adminGroups = adminUser.getAdminGroup();
        if (adminGroups.size() > 0) {
            for (int i = 0; i < adminGroups.size(); i++) {
                AdminGroup adminGroup = groupService.get(adminGroups.get(i).getId());
                adminGroups.set(i, adminGroup);
            }
        }
        return adminUser;
    }

    @Override
    public AdminUser validateByNickName(String username) {
        Map<String, Object> params = new HashMap<>();
        params.put("nickName", username);
        List<AdminUser> adminUser = this.query("find", params);
        if (adminUser.size() > 0) {
            return adminUser.get(0);
        }
        return null;
    }

    @Override
    public AdminUser validateByEmail(String email) {
        Map<String, Object> params = new HashMap<>();
        params.put("email", email);
        List<AdminUser> adminUser = this.query("find", params);
        if (adminUser.size() > 0) {
            return adminUser.get(0);
        }
        return null;
    }

    @Override
    public List<AdminUser> findListByEmail(String email) {
        Map<String, Object> params = new HashMap<>();
        params.put("email", email);
        List<AdminUser> adminUser = this.query("find", params);

        return adminUser;
    }

    @Override
    public boolean validateUser(String name, String password) {
        Map<String, Object> params = new HashMap<>();
        params.put("namePassword", name);
        params.put("password", password);
        AdminUser adminUser = (AdminUser) this.query("find", params);
        if (adminUser != null) {
            return true;
        }
        return false;
    }

    @Override
    public void changePassword(String password_old, String password_c) {
        UserDetailsUtils detailsUtils = new UserDetailsUtils();
        AdminUser adminUser = this.validateByName(detailsUtils.getPrincipal());

        String passwd = DigestUtils.sha1Hex(password_old);
        if (adminUser.getPassword().equals(passwd)) {
            adminUser.setPassword(DigestUtils.sha1Hex(password_c));
            this.update(adminUser);
        } else {
            throw new CredentialsException("旧密码不匹配");
        }
    }

    @Override
    public AdminUser regUser(AdminUser user, String password) {
        return null;
    }

    @Override
    public int insert(AdminUser t) {
        int result = super.insert(t);
        List<AdminGroup> groups = t.getAdminGroup();
        if (groups.size() > 0) {
            for (AdminGroup adminGroup : groups) {
                AdminUserGroup adminUserGroup = new AdminUserGroup();
                adminUserGroup.setGid(adminGroup.getId());
                adminUserGroup.setUid(t.getId());
                adminUserGroupService.insert(adminUserGroup);
            }
        }
        return result;
    }

    @Override
    public int update(AdminUser t) {

        if (t.getAdminGroup().size() > 0) {
            String uid = t.getId();
            adminUserGroupService.delete("delete", uid);
            List<AdminGroup> groups = t.getAdminGroup();
            for (AdminGroup adminGroup : groups) {
                AdminUserGroup adminUserGroup = new AdminUserGroup();
                adminUserGroup.setGid(adminGroup.getId());
                adminUserGroup.setUid(uid);
                // 需要插入的用户组
                adminUserGroupService.insert(adminUserGroup);
            }
        }

        return super.update(t);
    }

    @Override
    public int delete(String id) {
        adminUserGroupService.delete(id);
        return super.delete(id);
    }


    /**
     * 更改密码
     */
    @Override
    public int changePasswd(String username, String passwd) {
        AdminUser adminUser = this.validateByName(username);
        if (adminUser != null) {
            adminUser.setPassword(DigestUtils.sha1Hex(passwd));
            this.update(adminUser);
        }
        return 0;
    }

}